package com.badibadi.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import com.badibadi.activity.BaseActivity.OnMyDialogeClistener;
import com.badibadi.adapter.AreaAdapter;
import com.badibadi.infos.AreaModel;
import com.badibadi.infos.IsOAuthLoginModel;
import com.badibadi.infos.Results;
import com.badibadi.mytools.AreaDialog;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MySharePreferences;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.RegexUtils;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 第三方登录
 * @author Administrator
 *
 */
public class TheThirdPartyActivity extends BaseActivity {

	protected static final String TAG = "TheThirdPartyActivity";
	EditText et_sex;
	private List<AreaModel> areaModelscountry, areaModelssheng, areaModelscity;
	private String country = null, sheng = null, city = null, area_id = null,
			sex_temp = null;
	private AreaAdapter areaAdapter;
	private AreaDialog areaDialog;
	private TextView from;// 来自
	Button regirstration;
	EditText et_mail;
	private int languageType = 0;
	private MySharePreferences preferences;
	
	String uid;
	String Pwd;
	private static final String SaveUserName = "user";
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				// Is_Area_Dialog(RegirstrationActivity.this);
				areaDialog.AreaDisplayDialog();
				areaAdapter = new AreaAdapter(TheThirdPartyActivity.this,
						areaDialog.areaModelscountry);
				areaDialog.holderView.area_country_name_spinner
						.setAdapter(areaAdapter);
				// holderView.area_country_name_spinner.setAdapter(areaAdapter);
				break;
			case 2:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				Utils.showMessage(TheThirdPartyActivity.this, getResources().getString(R.string.l_net_error));
				break;
			case 3:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				areaAdapter = new AreaAdapter(TheThirdPartyActivity.this,
						areaDialog.areaModelssheng);
				areaDialog.holderView.area_province_name_spinner
						.setAdapter(areaAdapter);
				// holderView.area_province_name_spinner.setAdapter(areaAdapter);
				break;
			case 4:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				areaAdapter = new AreaAdapter(TheThirdPartyActivity.this,
						areaDialog.areaModelscity);
				areaDialog.holderView.area_city_name_spinner
						.setAdapter(areaAdapter);
				// holderView.area_city_name_spinner.setAdapter(areaAdapter);
				break;
			case 5:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				Utils.showMessage(TheThirdPartyActivity.this, msg.getData()
						.getString("注册"));
				Intent intent = new Intent(TheThirdPartyActivity.this,
						ValidationEmailActivity.class);
				intent.putExtra("aaa", "" + et_mail.getText().toString().trim());
				startActivity(intent);
				break;
			case 6:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				Utils.showMessage(TheThirdPartyActivity.this, msg.getData()
						.getString("注册"));
				break;
			case 7:
				String[] display = msg.getData().getString("来自", getResources().getString(R.string.l_x2))
						.split(" ");
				if (display != null && display.length > 0) {
					from.setText(display[0] + " " + display[1] + " "
							+ display[2]);
					area_id = display[3];
				} else {
					from.setText(getResources().getString(R.string.l_x2));
					area_id = null;
				}

				break;
			case 8:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				try {
					Utils.showMessage(TheThirdPartyActivity.this, getResources().getString(R.string.l_regirstration_scross));
					Intent intent3=new Intent(TheThirdPartyActivity.this,LoginActivity.class);
//					intent3.putExtra("isStartSocket", true);
//					intent3.putExtra("isStartTwoClose", true);
					TheThirdPartyActivity.this.startActivity(intent3);
				    Utils.showMessage(TheThirdPartyActivity.this, getResources().getString(R.string.l_xb55));
//					MySharePreferences preferences = new MySharePreferences(
//							TheThirdPartyActivity.this, SaveUserName);
////					preferences.put(USER_NAME_KEY, username);
////					preferences.put(PASS_WORD_KEY, password);
//					preferences.put("uid", uid);
//					preferences.put("Pwd", Pwd);
					exit();
//					startActivity(intent3);
				} catch (Exception e) {
					// TODO: handle exception
				}
		
				break;
			case 9:
				Utils.ExitPrgress(TheThirdPartyActivity.this);
				try {
					Utils.showMessage(TheThirdPartyActivity.this, getResources().getString(R.string.l_xb143));
				
					
				} catch (Exception e) {
					// TODO: handle exception
				}
		
				break;
			default:
				break;
			}
		}
	};
	
	private void Getareathread(final String type) {
		Utils.showPrgress(TheThirdPartyActivity.this);
		new Thread(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();

				int t = Integer.parseInt(type);
				switch (t) {
				case 0:
					map.put("languageType", "cn_name");
					break;
				case 1:
					map.put("languageType", "en_name");
					break;
				case 2:
					map.put("languageType", "tw_name");
					break;
				}
				String content = Utils.sendRequest(map, Constants.BadiUrl
						+ "area");
				if (content != null) {
					//    ("结果是:" + content);
					Results results = Utils.checkResult_NNN(
							getApplicationContext(), content);
					if (results != null && results.getRetmsg() != null) {
						//    ("得到的结果是：" + results.getRetmsg());
						try {
							areaModelscountry = Utils.getListByJsonString(
									results.getRetmsg(), AreaModel.class);
							handler.sendEmptyMessage(1);
						} catch (JSONException e) {
//							   ("解析出错");
							   
						}
					}
				} else {
					handler.sendEmptyMessage(2);
				}
			}
		}).start();
	}
	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_the_third_party);
		
		uid=getIntent().getStringExtra("uid");
		Pwd=getIntent().getStringExtra("Pwd");
		init();
		
		
	}
	private void init() {
		// TODO Auto-generated method stub
		//在这里注册
		regirstration=(Button)findViewById(R.id.regirstration);
		regirstration.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			     String mail=et_mail.getText().toString().trim();
			     String sex=et_sex.getText().toString().trim();
			    if(panduan())
			    {
			    	 String sex2="0";
					if(sex.equals(getResources().getString(R.string.l_x3)))
					{
						sex2="2";
					}else
						if(sex.equals(getResources().getString(R.string.l_x4)))
						{
							sex2="1";
						}else
							if(sex.equals(getResources().getString(R.string.l_xb144)))
							{
								sex2="0";
							}
//			    	zhuce();.//在这里注册
			    	saveInfo(uid,mail,sex2,area_id);
			    }
			     
			}
		});
		et_sex= (EditText) findViewById(R.id.sex);
		et_sex.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				setSexDialog(TheThirdPartyActivity.this);
				setOnMyDialogeClistener(new OnMyDialogeClistener() {

					@Override
					public void Ok() {
						// TODO Auto-generated method stub

						et_sex.setText("" + getSex());
						if (et_sex.getText().toString().trim().equals(getResources().getString(R.string.l_x3))) {
							sex_temp = "2";
						} else if (et_sex.getText().toString().trim()
								.equals(getResources().getString(R.string.l_x4))) {
							sex_temp = "1";
						} else {
							sex_temp = "0";
						}
					}

					@Override
					public void Cancle() {
						// TODO Auto-generated method stub

					}
				});

			}
		});
//		et_sex
		
		preferences = new MySharePreferences(getApplicationContext(),
				"language");
		languageType = preferences.get("language", 0);
		areaDialog = new AreaDialog(TheThirdPartyActivity.this, languageType,
				handler);
		
		et_mail=(EditText)this.findViewById(R.id.mail);		
		
		from = (TextView) this.findViewById(R.id.from);
		from.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// Getareathread("" + languageType);// 主动获取国家数据
				// setFromDialog(RegirstrationActivity.this);
				Utils.showPrgress(TheThirdPartyActivity.this);
				areaDialog.Getareathread();
			}
		});
	}
	protected void saveInfo(final String uid2, final String mail, final String sex,
			String area_id2) {
		// TODO Auto-generated method stub
	
		
		Utils.showPrgress(TheThirdPartyActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", uid2);
				map.put("email", mail);
				map.put("sex", sex);
				map.put("area_id", area_id);
				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "OAuth/saveInfo");
				// String content = Utils.getRequest(Constants.BadiUrl
				// + "record_info?rid=" +
				// rid+"&uid="+Utils.getUid(LoginActivity.this));
				//    (Constants.BadiUrl + "friends?uid=" + id
				// + "&page=" + page + "&pageNum=" + 10);
				if (content != null) {
					Results results = Utils.checkResult_NNN(TheThirdPartyActivity.this,
							content);
//					   
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
//							isOAuthLoginModel = JSONUtils
//									.getEntityByJsonString(results.getRetmsg(),
//											IsOAuthLoginModel.class);
							if(results.isRet()==true)
							{
								handler.sendEmptyMessage(8);
							}else
							{
								handler.sendEmptyMessage(9);
							}
							
							
						} catch (Exception e) {
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
		
		
		
		
		
		
	}
	protected boolean panduan() {
		// TODO Auto-generated method stub
		 if(RegexUtils
					.validateEmail(et_mail.getText().toString().trim()) == false)
	     {
	    		Toast.makeText(TheThirdPartyActivity.this, getResources().getString(R.string.l_xa5),
						Toast.LENGTH_SHORT).show();
				return false;
	     }else if ("".equals(et_sex.getText().toString().trim()) == true) {
	    	 
				Toast.makeText(TheThirdPartyActivity.this, getResources().getString(R.string.l_xa6),
						Toast.LENGTH_SHORT).show();
				return false;
	    	 
	     }else
	    	 if ("".equals(from.getText().toString().trim()) == true) {
	 			Toast.makeText(TheThirdPartyActivity.this, getResources().getString(R.string.l_xa7),
	 					Toast.LENGTH_SHORT).show();
	 			return false;
	 		}
		return true;
	}
}
